﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Products;

@model ProductListModel

<div class="row">
    <div class="col-md-2">
        <div class="input-group mb5">
            <dcms-select-bool asp-for="StockQtyMoreThan" asp-items="@(new string[] {  "是", "否"})" asp-default="只显示库存商品" />
        </div>
    </div>
    <div class="col-md-10">
        <div class="input-group mb5">
            <input type="text" id="txt_SearchProductStr" class="form-control" placeholder="请输入商品名称、助记名..." onkeydown="if(event.keyCode==13){event.keyCode=0;event.returnValue=false;}">
            <span class="input-group-btn">
                <button id="btn_product_query" type="button" class="btn btn-primary" style="border:1px solid #dddddd;"><span class="glyphicon glyphicon-search"></span> 搜索</button>
            </span>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-2 prn">
        <div class="panel">
            <div class="panel-heading">
                <div class="pull-left">
                    商品类别
                </div>
            </div>
            <div class="panel-body pn">
                <input type="hidden" name="ProductCategoryId" id="ProductCategoryId" value="0" />
                <div id="CatagoriesTreeData"></div>
            </div>
        </div>
    </div>
    <div class="col-md-10">
        <table class="table table-striped table-hover" id="products_datatable"></table>
    </div>
</div>

<script type="text/javascript">

    //保存选择
    $("#productSelect_btnSave").click(function () {
        var arrselections = $.map($("#products_datatable").bootstrapTable('getSelections'), function (row) {
            return row.Id
        });
        if (arrselections.length <= 0) {
            $(this).showToastr('请选择有效数据');
            return;
        }
    });

    $(function () {

        $('body').on('shown.bs.modal', function () {
            $("#products_datatable").bootstrapTable('refresh');
        });

        $('body').on('hide.bs.modal', function () {
            document.onkeydown = undefined;
        });

         //商品类别树
        $("#CatagoriesTreeData").fancytree({
            extensions: ["dnd", "edit"],
            source: {
                url: "/Category/GetFancyTree"
            },
            dnd: {
                autoExpandMS: 400,
                focusOnClick: true,
                preventVoidMoves: true,
                preventRecursiveMoves: true,
                dragStart: function (node, data) {
                    return true;
                },
                dragEnter: function (node, data) {
                    return true;
                },
                dragDrop: function (node, data) {
                    data.otherNode.moveTo(node, data.hitMode);
                }
            },
            activate: function (event, data) {
            },
            lazyLoad: function (event, data) {
                data.result = $.ajax({
                    url: "/Category/GetFancyTree",
                    dataType: "json"
                });
            },
            click: function (event, data) {
                //var node = $("#CatagoriesTreeData").fancytree("getActiveNode");
                var node = data.node.data;
                //console.log(node);
                $("#ProductCategoryId").val(node.id);
                //logEvent(event, data, ", targetType=" + data.targetType);
                $("#products_datatable").bootstrapTable('refresh');
                //$("#ProductCategoryId").val("0");
            },


        });
         //商品列表
        $("#products_datatable").bootstrapTable({
            url: "/Product/AsyncList",
            method: 'get',
            striped: true,
            cache: false,
            pagination: true,
            queryParams: function (params)
            {
                var temp =
                {
                    key: $("#txt_SearchProductStr").val(),
                    categoryId: $("#ProductCategoryId").val(),
                    pageSize: params.limit,
                    pageIndex: (params.offset / params.limit),
                    excludeIds: "@(string.Join(",", Model.ExcludeIds.ToArray()))",
                    wareHouseId: "@Model.WareHouseId",
                    stockQtyMoreThan: $("#StockQtyMoreThan").val(),
                    IncludeProductDetail: "@(Model.IncludeProductDetail ? 1:0 )",
                    terminalId: "@Model.TerminalId",
                    productStatus: 1
                };
                return temp;
            },
            sidePagination: "server",
            idField: 'Id',
            dataField: "Rows",
            totalField: "Total",
            pageNumber: 1,
            pageSize: 10,
            search: false,
            strictSearch: true,
            showRefresh: false,
            singleSelect: @Model.SingleSelect.ToString().ToLower(),
            clickToSelect: false,
            uniqueId: "Id",
            showToggle: false,
            cardView: false,
            detailView: false,
            columns: [
                { checkbox: true },
                { field: 'Name', title: '商品名称',align: 'left' },
                {
                    field: 'CurrentQuantityConversion', title: '现货库存数量',align: 'right',
                    formatter: function (value, row, index) {
                        if (row.CurrentQuantity > 0) {
                            return value;
                        } else {
                            return '<span class="label label-danger">库存不足</span>';
                        }
                    }
                },
                {
                    field: 'UsableQuantityConversion', title: '可用库存数量',align: 'right',
                    formatter: function (value, row, index) {
                            if (row.UsableQuantity > 0) {
                            return value;
                        } else {
                            return '<span class="label label-danger">库存不足</span>';
                        }
                    }
                },
                { field: 'BrandName', title: '品牌', align: 'left' },
                {
                    field: 'Status', title: '状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value) {
                            return '<span class="label label-success">正常</span>';
                        } else {
                            return '<span class="label label-default">锁定</span>';
                        }
                    }
                },
                { field: 'UnitName', title: '单位', align: 'left' },
                { field: 'UnitConversion', title: '规格', align: 'left' }, 
                {
                    field: 'operate', title: '操作', align: 'center', width: '100',
                    events: operateEventsPop,
                    formatter: function (value, row, index)
                    {
                        if (row.CurrentQuantity > 0 || $("#ShowStockProducts").val() == 'false' || $("#StockQtyMoreThan").val() == 'False' || "@Model.BillType" == "14")
                        {
                                return [
                            '<button type="button" class="btn btn-sm btn-info rowSelect" data-id="@Model.RowIndex" data-target="@Model.Target" data-targetBillTypeillTypeBillTypeform="@Model.TargetForm"><i class="fa fa-check"></i>&nbsp;选择</button>'
                        ].join('');
                        }
                        else
                        {
                            return [
                            '<button type="button" class="btn btn-sm btn-default" disabled><i class="fa fa-check"></i>&nbsp;选择</button>'
                        ].join('');
                        }
                    }
                }
            ],
            onPageChange: function (number, size) {
                m_pagerow = (number - 1) * size;
            },
            onLoadSuccess: function (data) {
                //console.log('load');
            }
        });
        //只显示库存商品
        $("#StockQtyMoreThan").change(function () {
              $("#products_datatable").bootstrapTable('refresh');
        });

        var searchProduct = function ()
        {
            var keys = $("#txt_SearchProductStr").val();
            if (keys == "") {
                $(this).showToastr('请输入关键字！');
                return false;
            }
            $("#products_datatable").bootstrapTable('refresh');
        }

        //查询
        $("#btn_product_query").on('click', function ()
        {
            searchProduct();
        });

        $("#btn_product_query").keypress(function (even) {
            if (even.which == 13) {
                searchProduct();
            }
        });

        document.onkeydown = function (event)
        {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 27) { // 按 Esc

            }
            if (e && e.keyCode == 113) { // 按 F2

            }
            if (e && e.keyCode == 13)
            {
                // enter 键
                searchProduct();
            }
        };
    });
</script>